package org.example.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.bean.Address;
import org.example.service.AddressService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Tag(name = "地址管理",description = "提供对地址的增删改查操作")
public class AddressController {
    private final AddressService addressService;
    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }
    @PostMapping("/add")
    @Operation(summary = "添加地址",description = "根据用户id添加地址")
    public ResponseEntity<ResponseResult> addAddress(@RequestParam("user_id") Integer user_id,
                                                     @RequestParam("recipient_name") String recipient_name,
                                                     @RequestParam("phone") String phone,
                                                     @RequestParam("detailed_address") String detailed_address){
        Address address=new Address();
        address.setUser_id(user_id);
        address.setRecipient_name(recipient_name);
        address.setPhone(phone);
        address.setDetailed_address(detailed_address);
        address.setIs_default(0);
        address.setAddress_status(1);
        addressService.addAddress(address);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("添加成功").data(address).build());
    }

    @GetMapping("/getAddressesByStatus/{address_status}")
    @Operation(summary = "根据状态查询地址",description = "根据地址状态查询地址")
    public ResponseEntity<ResponseResult> getAddressesByStatus(@PathVariable("address_status") Integer address_status){
        List<Address> addresses = addressService.getAddressesByStatus(address_status);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(addresses).build());
    }

    //根据地址索引更新为是否是默认地址
    @PutMapping("/updateIsDefault/{address_id}/{is_default}")
    @Operation(summary = "更新地址为默认地址",description = "根据地址索引更新为是否是默认地址")
    public ResponseEntity<ResponseResult> updateIsDefault(@PathVariable("address_id") Integer address_id,
                                                          @PathVariable("is_default") Integer is_default){
        boolean result = addressService.updateIsDefault(address_id, is_default);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("更新成功").data(result).build());
    }
    //根据地址索引改变地址的状态
    @PutMapping("/updateAddressStatus/{address_id}/{address_status}")
    @Operation(summary = "更新地址状态",description = "根据地址索引改变地址的状态")
    public ResponseEntity<ResponseResult> updateAddressStatus(@PathVariable("address_id") Integer address_id,
                                                              @PathVariable("address_status") Integer address_status){
        boolean result = addressService.updateAddressStatus(address_id, address_status);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(result).build());
    }
    //根据地址索引修改地址信息
    @PutMapping("/updateAddress/{address_id}")
    @Operation(summary = "根据索引修改地址信息",description = "根据地址索引修改地址信息")
    public ResponseEntity<ResponseResult> updateAddress(@PathVariable("address_id") Integer address_id,
                                                        @RequestParam("user_id") Integer user_id,
                                                        @RequestParam("recipient_name") String recipient_name,
                                                        @RequestParam("phone") String phone,
                                                        @RequestParam("detailed_address") String detailed_address){
        Address address=new Address();
        address.setUser_id(user_id);
        address.setRecipient_name(recipient_name);
        address.setPhone(phone);
        address.setDetailed_address(detailed_address);
        boolean result = addressService.updateAddress(address_id, address);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(result).build());
    }

    //查找默认地址的地址信息
    @Operation(summary = "查找默认地址的地址信息",description = "根据是否是默认地址查找默认地址的地址信息")
    @GetMapping("/getDefaultAddress/{is_default}")
    public ResponseEntity<ResponseResult> getDefaultAddress(@PathVariable("is_default") Integer is_default){
        Address address = addressService.getDefaultAddress(is_default,1);
        if(address==null){
            return ResponseEntity.ok(ResponseResult.builder().code(0).msg("查询失败").data(null).build());
        }
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(address).build());
    }

}
